競プロ典型 90 問 002
種別: 記事
カテゴリ: 競技プログラミング
サブカテゴリ: AtCoder > 競プロ典型 90 問
タグ: #解いた問題
(工事中)
AtCoder で公開されている競プロ典型 90 問 の002 を解いた
解き方
解答例
下は上記の方法で解いたときの提出結果である。また、その提出の際に提出したソースコードをその下に転記する。
code: C
#include <stdio.h>
#include <stdlib.h>
void func(int i, int l, int r, int opened, char *s) {
if (l <= 0 && r <= 0) {
si = '\0';
printf("%s\n", s);
return;
}
if (l > 0) {
si = '(';
func(i+1, l-1, r, opened+1, s);
}
if (r > 0 && opened > 0) {
si = ')';
func(i+1, l, r-1, opened-1, s);
}
return;
}
int main () {
int n = 0;
char s21 = "";
int res = 0;
res = scanf("%d", &n);
if (n % 2 > 0) {
return 0;
}
func(0, n / 2, n / 2, 0, s);
return 0;
}
私の提出一覧
table: submissions_atcoder_typical90_002
提出のURL 提出時刻 結果 備考
1回目 https://atcoder.jp/contests/typical90/submissions/22258137 2021-05-02T17:56:04+0900 AC
感想
ローカルにおいてあったzakkan.txt(雑感)に書かれていたこと
002 - Encyclopedia of Parentheses(★3)
日時: 2021-05-02 17:56:04
提出: https://atcoder.jp/contests/typical90/submissions/22258137
条件を満たす文字列を作るためには、前から文字を決めていき、
それまでにそれぞれの括弧を何個使ったかを覚えておけば良いことに気づけば
あとは単純に再帰関数で順番に作成するだけなので特に難しく感じなかった